
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>

// https://www.ibm.com/developerworks/community/blogs/58e72888-6340-46ac-b488-d31aa4058e9c/entry/understanding_linux_open_system_call?lang=en

void main1(){
	int fd = open("./chap3_1.c",O_RDONLY);
	printf("%d\n", fd);
}

void main2(){
	int fd = open("./chap3_1.c",O_RDONLY);
	printf("%d\n", fd);
	if(-1 == fd) {
		printf("open() failed\n");
		return;
	}

	int code = close(fd);
	if( 0 == code)	{
		printf("close success\n");
	}	else	{
		printf("close failed\n");
	}
}

int  main3(char* file, int mode){
	int fd = open(file,mode);
	printf("%d\n", fd);
	
	if(-1 == fd) {
		printf("open() failed [%s]\n",file);
		
		return -1;
	}
	return fd;
	// close fd ...
}


int main(int argc, char* argv[]){
	// main1()
	// main2()
	// main3("chap3_1_open.c",O_RDONLY);
	// main3("adfasdf.c",O_RDONLY); // not exist
	if( 2 != argc ){
		printf("\n Usage :  \n");
        return 1;
	}
	// int fd = main3(argv[1],O_RDONLY);
	int fd = main3(argv[1],O_RDONLY|O_CREAT);
}